using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Jamila2.Tools;

public partial class GonzalesToolsProject : System.Web.UI.Page
{
    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;

    #region ProjectID

    protected string ProjectID
    {
        get
        {
            return ViewState["ProjectID"].ToString();
        }

        set
        {
            ViewState["ProjectID"] = value;
        }
    }

    #endregion

    #region EntityID

    protected string EntityID
    {
        get
        {
            return ViewState["EntityID"].ToString();
        }

        set
        {
            ViewState["EntityID"] = value;
        }
    }

    #endregion

    #region EmployeeCode

    protected string EmployeeCode
    {
        get
        {
            return ViewState["EmployeeCode"].ToString();
        }

        set
        {
            ViewState["EmployeeCode"] = value;
        }
    }

    #endregion    

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 6)
            {
                this.EmployeeCode = Request.QueryString["id"].ToString();
            }
            else
                this.EmployeeCode = string.Empty;

            this.PageComponentSetup();
            this.SwitchLanguage();
        }
    }

    protected void DropDownList_Entity_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.LoadData();
    }

    #endregion

    #region SessionCheck
    public void SessionCheck()
    {
        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.Label_Destination.Text = CurLanguageFactory.DisplayValue("Destination");
                this.Label_ModeOfTransport.Text = CurLanguageFactory.DisplayValue("ModeOfTransport");
                this.Label_Title.Text = CurLanguageFactory.DisplayValue("Title");
                this.Button_Cancel.Text = CurLanguageFactory.DisplayValue("Cancel");
                this.Button_Submit.Text = CurLanguageFactory.DisplayValue("Submit");
            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesToolsTaxi.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        this.Button_Cancel.Attributes["onclick"] = "if(validate()){parent.ModalWindow.hide();}return false;";
        this.Button_Submit.Attributes["onclick"] = "parent.ModalWindow.hide();return false;";

        // setup entity
        using (EntityManager CurEntityManager = new EntityManager(this.CurrentPrivateSession))
        {
            MasterBaseCollection ResultList = CurEntityManager.GetAllEntity();
            if (ResultList != null)
            {
                this.DropDownList_Entity.Items.Add(new ListItem("", ""));
                for (int x = 0; x < ResultList.Count; x++)
                {
                    this.DropDownList_Entity.Items.Add(new ListItem(ResultList[x].DisplayName, ResultList[x].InternalID));
                }
            }
            else
            {
                // display error
                this.CurLogManager.LogError("[GonzalesToolsProject.PageComponentSetup] : Failed on CurEntityManager.GetAllEntity() : " + CurEntityManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);

            }
        }
    }
    #endregion

    #region Data Operation

    protected void LoadData()
    {
        if (this.DropDownList_Entity.SelectedIndex > 0)
        {
            using (ProjectManager CurProjectManager = new ProjectManager(this.CurrentPrivateSession))
            {
                // Thang Nguyen edited 26/03/2013
                MasterBaseCollection ProjectCollection = null;
                if (this.EmployeeCode.Equals(""))
                {
                    ProjectCollection = CurProjectManager.GetProjectByEntityCode(this.DropDownList_Entity.SelectedValue);
                    if (ProjectCollection != null)
                    {
                        this.DropDownList_Project.Items.Clear();

                        for (int x = 0; x < ProjectCollection.Count; x++)
                        {
                            Project CurProject = (Project)ProjectCollection[x];

                            this.DropDownList_Project.Items.Add(new ListItem(CurProject.ProjectID + " - " + CurProject.ProjectDescription, CurProject.ProjectID));
                        }
                    } 
                }
                else
                {
                    ProjectCollection = CurProjectManager.GetProjectByEntityEmployeeCode(this.DropDownList_Entity.SelectedValue, this.EmployeeCode);
                    if (ProjectCollection != null)
                    {
                        this.DropDownList_Project.Items.Clear();

                        for (int x = 0; x < ProjectCollection.Count; x++)
                        {
                            CRMOD CurProject = (CRMOD)ProjectCollection[x];

                            this.DropDownList_Project.Items.Add(new ListItem(CurProject.ProjectID + " - " + CurProject.ProjectName, CurProject.ProjectID));
                        }
                    }
                }
            }
        }
    }

    #endregion
}
